package com.huobao.yunying.activitys;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.BitmapTools;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.Utils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.Log2File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;
import net.bither.util.NativeUtil;

/**
 * 上传头像
 * 
 * @author Administrator
 *
 */
public class Act_Manager_UserPhoto extends HuobaoBaseActivity {

	@ViewInject(R.id.imgView)
	private ImageView imgView;
	private String mSelectPhoto;
	private String user_id;
	@ViewInject(R.id.tv_select_photo)
	private TextView tv_select_photo;

	@Override
	protected int getContentView() {
		return R.layout.layout_manager_photo;
	}

	@Override
	protected String getTitleText() {
		return "取消";
	}

	@Override
	protected void init() {
		user_id = getIntent().getStringExtra("user_id");
		tv_select_photo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				onSelectImage();
			}
		});
	}

	public void onSubmitClick(View view) {
		Bitmap bitmap = BitmapTools.createImageThumbnail(mSelectPhoto);
		if (bitmap == null)
			return;
		File f = new File(mSelectPhoto);
		File newPath = new File(f.getParent(), f.getName() + "temp" + ".jpg");
		NativeUtil.compressBitmap(bitmap, 100, newPath.getAbsolutePath(), true);
		bitmap.recycle();
		String base64str = Base64.encodeToString(Utils.File2byte(newPath.getAbsolutePath()), Base64.NO_WRAP);
		Log2File.newInstant().log2File(base64str);
		Map<String, String> map = HttpUtil.getParams("app", "uploadpic", "act", "gravatar", "user_id", user_id, "portrait",
				base64str);
		Http.getInstant().sendRequest(context, true, HttpConst.UPLOAD_PHOTO_URL, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				String photo = jsonObject.getString("retval");
				EventBus.getDefault().post(photo, "uploadPhotoSuccess");
				finish();
			}

			@Override
			public void onHttpError(String error) {
               
			}
		});
	}

	private void onSelectImage() {
		Intent intent = new Intent(this, MultiImageSelectorActivity.class);
		// 是否显示拍摄图片
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
		// 最大可选择图片数量
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
		// 选择模式
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
		// // 默认选择
		// if(mSelectPath != null && mSelectPath.size()>0){
		// intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST,
		// mSelectPath);
		// }
		startActivityForResult(intent, 1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				List<String> mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
				if (mSelectPath != null && mSelectPath.size() != 0) {
					mSelectPhoto = mSelectPath.get(0);
					showSelectPic(mSelectPhoto);
				}
			}
		}
	}

	private void showSelectPic(String string) {
		ImageUtils.loadimgFromSDcard(imgView, string);
	}
}
